package com.yuma.tingshu.dispatch.job;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yuma.tingshu.common.result.Result;
import com.yuma.tingshu.dispatch.mapper.XxlJobLogMapper;
import com.yuma.tingshu.model.dispatch.XxlJobLog;
import com.yuma.tingshu.search.client.SearchFeignClient;
import com.yuma.tingshu.user.client.UserInfoFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class DispatchJobHandler {

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private SearchFeignClient searchFeignClient;

    @Autowired
    private XxlJobLogMapper xxlJobLogMapper;

    private static Logger logger = LoggerFactory.getLogger(DispatchJobHandler.class);

    // 1.VIP订单修改的job
    @XxlJob("updateVipExpireStat")
    public void updateVipExpireStat() {
        logger.info("开始执行任务");
        XxlJobHelper.log("开始执行更新vip状态任务的job");
        // 获取任务执行的初始时间
        long startTime = System.currentTimeMillis();
        XxlJobLog xxlJobLog = new XxlJobLog();
        // 设置任务id
        xxlJobLog.setJobId(XxlJobHelper.getJobId());
        try {
            // 核心调用远程接口执行任务更新vip
            Result result = userInfoFeignClient.updateVipExpireStat();
            if (result == null) {
                xxlJobLog.setStatus(0);
                xxlJobLog.setError("更新vip状态远程调用失败");
                XxlJobHelper.handleFail("更新vip状态远程调用失败");
            } else {
                if (result.getCode().intValue() == 200) {
                    xxlJobLog.setStatus(1);
                    XxlJobHelper.handleSuccess("更新vip状态远程调用成功");
                } else {
                    xxlJobLog.setStatus(0);
                    XxlJobHelper.handleFail("更新VIP状态时失败码:" + result.getCode());
                }
            }
        } catch (Exception e) {
            xxlJobLog.setStatus(0);
            xxlJobLog.setError("更新vip状态远程调用失败");
            XxlJobHelper.handleFail("更新vip状态远程调用失败");
            e.printStackTrace();
        } finally {
            // 记录到日志 xxl_job_log
            int times = (int) (System.currentTimeMillis() - startTime);
            xxlJobLog.setTimes(times);
            this.xxlJobLogMapper.insert(xxlJobLog);
            logger.info("更新vip状态的job执行完毕");
            XxlJobHelper.log("更新vip状态的job执行完毕");
        }
    }

    @XxlJob("syncDataToEs")
    public void syncDataToEs() {
        log.info("更新专辑统计到es：{}", XxlJobHelper.getJobId());
        // 获取任务执行的初始时间
        long startTime = System.currentTimeMillis();
        XxlJobLog xxlJobLog = new XxlJobLog();
        // 设置任务id
        xxlJobLog.setJobId(XxlJobHelper.getJobId());
        try {
            Result result = this.searchFeignClient.syncDataToEs();
            // 如果result为空，说明远程调用失败
            if (result == null) {
                xxlJobLog.setStatus(0);
                xxlJobLog.setError("更新专辑状态远程调用失败");
                XxlJobHelper.handleFail("更新专辑状态远程调用失败");
            } else {
                if (result.getCode().intValue() == 200) {
                    xxlJobLog.setStatus(1);
                    XxlJobHelper.handleSuccess("更新专辑状态远程调用成功");
                } else {
                    xxlJobLog.setStatus(0);
                    XxlJobHelper.handleFail("更新专辑状态时失败码:" + result.getCode());
                }
            }
        } catch (Exception e) {
            xxlJobLog.setStatus(0);
            xxlJobLog.setError("更新专辑状态远程调用失败");
            XxlJobHelper.handleFail("更新专辑状态远程调用失败");
            e.printStackTrace();
        } finally {
            // 记录执行时间
            xxlJobLog.setTimes((int) (System.currentTimeMillis() - startTime));
            this.xxlJobLogMapper.insert(xxlJobLog);
            logger.info("更新专辑状态的job执行完毕");
            XxlJobHelper.log("更新专辑状态的job执行完毕");
        }
    }
}